{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generic imports\n",
    "import pandas as pd\n",
    "import plotly\n",
    "\n",
    "# 5G visualization logic\n",
    "import trace_plotting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output from kubectl get --raw /apis/metrics.k8s.io/v1beta1/pods/. Each line a JSON output\n",
    "filename = 'traces/test106/test106_kpidump.log'\n",
    "\n",
    "# Wireshark trace of NAS messages\n",
    "wireshark_traces = [\n",
    "    'traces/test106/test106_tcpdump_AMF_N1N2.pcap',\n",
    "    'traces/test106/test106_tcpdump_SMF_N4.pcap',\n",
    "    'traces/test106/test106_tcpdump_SMF_SBI.pcap'\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_to_plot = trace_plotting.parse_k8s_kpis_as_dataframe(filename)\n",
    "display(data_to_plot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import Wireshark capture\n",
    "packets_df = trace_plotting.import_pcap_as_dataframe(\n",
    "    wireshark_traces, \n",
    "    http2_ports = \"32445,5002,5000,32665,80,32077,5006,8080,3000,8081\",\n",
    "    wireshark_version = 'latest')\n",
    "display(packets_df)\n",
    "wireshark_imported = packets_df is not None\n",
    "one_wireshark_trace = len(packets_df.file.unique()) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Placeholder to check some values\n",
    "# pd.set_option('display.max_rows', None)\n",
    "# df[(df['namespace']=='amf') & (df['timestamp']=='2021-01-29 09:59:06+00:00')]\n",
    "display(packets_df)\n",
    "# display(packets_df.summary.unique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import plotly.graph_objects as go\n",
    "from plotly.subplots import make_subplots\n",
    "import re\n",
    "\n",
    "# Just an example how to generate the title\n",
    "title = 'Test {0}'.format(re.search(r'[\\d]+', filename).group(0))\n",
    "colors = plotly.colors.DEFAULT_PLOTLY_COLORS\n",
    "#display(data_to_plot)\n",
    "\n",
    "# fig = go.Figure()\n",
    "# fig = make_subplots(specs=[[{\"secondary_y\": True}]])\n",
    "if not wireshark_imported:\n",
    "    fig = make_subplots(\n",
    "        rows=3, \n",
    "        cols=1,\n",
    "        shared_xaxes=True,\n",
    "        vertical_spacing=0.05)\n",
    "else:\n",
    "    fig = make_subplots(\n",
    "        rows=4, \n",
    "        cols=1,\n",
    "        shared_xaxes=True,\n",
    "        vertical_spacing=0.05,\n",
    "        row_heights=[0.2, 0.2, 0.2, 0.4])\n",
    "\n",
    "for idx,namespace in enumerate(data_to_plot['namespace'].unique()):\n",
    "    series = data_to_plot[data_to_plot['namespace']==namespace]\n",
    "    data_text = namespace + ', ' + series['timestamp'].apply(trace_plotting.datetime_to_str)\n",
    "    series_color = colors[idx%len(colors)]\n",
    "    for idx, column in enumerate(['cpu', 'memory', 'pod count']):\n",
    "        fig.add_trace(trace_plotting.generate_scatterplot_for_k8s_kpis(series, namespace, idx==0, data_text, series_color, column),\n",
    "                      row=idx+1, col=1)\n",
    "    \n",
    "if wireshark_imported:\n",
    "    for wireshark_plot in trace_plotting.generate_scatterplots_for_wireshark_traces(packets_df, filter_column='file'):\n",
    "        fig.add_trace(wireshark_plot, row=4, col=1)\n",
    "\n",
    "fig.update_traces(\n",
    "    marker_size=4,\n",
    "    hoverlabel_align = 'right')\n",
    "fig.update_yaxes(title_text='CPU [vCPU]', row=1, col=1)\n",
    "fig.update_yaxes(title_text='Memory [GB]', row=2, col=1)\n",
    "fig.update_yaxes(title_text='Pod count', row=3, col=1)\n",
    "if not wireshark_imported:\n",
    "    fig['layout'].update(height=700)\n",
    "    fig.update_xaxes(title_text='Date', row=3, col=1)\n",
    "else:\n",
    "    fig['layout'].update(height=1000)\n",
    "    fig.update_xaxes(title_text='Date', row=4, col=1)\n",
    "# fig.update_layout(hovermode='x unified')\n",
    "fig.update_layout(title_text=title)\n",
    "\n",
    "if wireshark_imported:\n",
    "    # Add color layout to procedure plot\n",
    "    fig.update_layout(shapes=trace_plotting.get_protocol_shapes(packets_df))\n",
    "\n",
    "fig.show()\n",
    "fig.write_html(\"{0}.html\".format(title))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
